package com.authsphere.security.common.logging;

import com.authsphere.logging.common.collector.LogCollector;
import com.authsphere.logging.common.entity.LogDetail;
import com.authsphere.security.common.logging.factory.LogDetailFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/13 21:09
 **/
public abstract class LoggingAuthenticationEntryPoint implements AuthenticationEntryPoint {
    public LoggingAuthenticationEntryPoint(LogCollector<LogDetail> logCollector) {
        this.logCollector = logCollector;
    }

    private final LogCollector<LogDetail> logCollector;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        try {
            logCollector.collect(LogDetailFactory.build(request, response, null, null, exception));
        } finally {
            doCommence(request, response, exception);
        }
    }

    protected abstract void doCommence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception);

}
